package com.googlecode.publicholiday.repository.oracle;

import com.googlecode.publicholiday.repository.MutablePublicHolidayRepository;
import com.googlecode.publicholiday.utils.SqlUtils;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.sql.DataSource;
import java.sql.*;

public class OraclePublicHolidayRepository extends OracleImmutablePublicHolidayRepository implements MutablePublicHolidayRepository {

    private final static Logger LOG = LoggerFactory.getLogger(OraclePublicHolidayRepository.class);

    public OraclePublicHolidayRepository(DataSource dataSource) {
        super(dataSource);
    }

    @Override
    public boolean create(String countryCode, LocalDate holidayDate) {
        boolean inserted = find(countryCode, holidayDate) == null;
        if (inserted) {
            Connection connection = null;
            CallableStatement statement = null;
            try {
                LOG.debug("Attempting to create a Public Holiday in: {} on: {}", countryCode, holidayDate);
                connection = getDataSource().getConnection();
                statement = connection.prepareCall("{ ? = public_holiday_pkg.create_holiday( ?, ? ) }");
                statement.registerOutParameter(1, Types.BOOLEAN);
                statement.setString(2, countryCode);
                statement.setDate(3, new Date(holidayDate.toDate().getTime()));
                statement.executeUpdate();
                inserted = statement.getBoolean(1);
                connection.commit();
            } catch (SQLException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } finally {
                SqlUtils.close(statement);
                SqlUtils.close(connection);
            }
        } else {
            LOG.debug("Public Holiday already exists in: {} on: {}", countryCode, holidayDate);
        }
        return inserted;
    }

    @Override
    public boolean delete(String countryCode, LocalDate holidayDate) {
        Connection connection = null;
        CallableStatement statement = null;
        boolean deleted = true;
        try {
            LOG.debug("Attempting to delete a Public Holiday in: {} on: {}", countryCode, holidayDate);
            connection = getDataSource().getConnection();
            statement = connection.prepareCall("{ ? = public_holiday_pkg.delete_holiday( ?, ? ) }");
            statement.registerOutParameter(1, Types.BOOLEAN);
            statement.setString(2, countryCode);
            statement.setDate(3, new Date(holidayDate.toDate().getTime()));
            statement.executeUpdate();
            deleted = statement.getBoolean(1);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } finally {
            SqlUtils.close(statement);
            SqlUtils.close(connection);
        }
        return deleted;
    }
}
